﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ZXing;

namespace 生成二维码
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            this.textBox1.Text = "M2019000001";
        }
        private string m = "";

        private void button1_Click(object sender, EventArgs e)
        {
            string str = textBox1.Text.ToString();
            m = str.Substring(0, 1);
            string num = str.Substring(1);
            int count = Convert.ToInt32(textBox2.Text.ToString());
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < count; i++)
            {
                sb.Append(Convert.ToInt32(num) + (i) + "\r\n");
            }

            txtresult.Text = sb.ToString();


        }

        private void button2_Click(object sender, EventArgs e)
        {
            string[] str = txtresult.Text.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.None);
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] != "")
                {
                    string url = txturl.Text.ToString() + m + str[i];
                    // GenByZXingNet(url).Save(m+str[i]+".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);


                    string headerFileUrl = @"F:\yangshaodong\c#\生成二维码\生成二维码\bin\Debug\paywith.png";
                   
                    string saveFileUrl = string.Format(@"E:\RQCode\{0}.png", m + str[i]);
                    Bitmap qrCodeBitmap = GenByZXingNet(url);
                    Bitmap headerBitmap = new Bitmap(headerFileUrl);
                    Bitmap qrAndHeaderBitmap = QRCodeHelper.MergeQrImg(qrCodeBitmap, headerBitmap);
                    qrAndHeaderBitmap.Save(saveFileUrl, System.Drawing.Imaging.ImageFormat.Png);
                    AddTextToImg(m + str[i], qrAndHeaderBitmap, saveFileUrl);
                }



            }

            MessageBox.Show("生成完成");
        }

        private Bitmap GenByZXingNet(string msg)
        {
            BarcodeWriter writer = new BarcodeWriter();
            writer.Format = BarcodeFormat.QR_CODE;
            writer.Options.Hints.Add(EncodeHintType.CHARACTER_SET, "UTF-8");//编码问题
            writer.Options.Hints.Add(
                EncodeHintType.ERROR_CORRECTION,
                ZXing.QrCode.Internal.ErrorCorrectionLevel.H

            );
            const int codeSizeInPixels = 250;   //设置图片长宽
            writer.Options.Height = writer.Options.Width = codeSizeInPixels;
            writer.Options.Margin = 0;//设置边框
            ZXing.Common.BitMatrix bm = writer.Encode(msg);
            Bitmap img = writer.Write(bm);
            return img;
        }

        private void AddTextToImg(string text, Bitmap bitmap, string filename)
        {

            Bitmap newbitmap = new Bitmap(250, 280);
            using (Graphics g = Graphics.FromImage(newbitmap))
            {
                g.Clear(System.Drawing.Color.White);
                Point mm = new Point(0, 0);
                g.DrawImage(bitmap, mm);

                g.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
                using (Font f = new Font("黑体", 20))
                {
                    using (Brush b = new SolidBrush(Color.Black))
                    {
                        string addText = text;
                        g.DrawString(addText, f, b, 50, 250);
                    }
                }
            }
            System.Drawing.Bitmap objNewPic = new System.Drawing.Bitmap(newbitmap, 449, 472);
            objNewPic.Save(filename, ImageFormat.Jpeg);
           // newbitmap.Save(filename, System.Drawing.Imaging.ImageFormat.Png);

        }


    }
}
